<template>
  <a-form :form="form">
    <div v-for="(item, index) in empWork.empWorkList" :key="item.guid || item.id">
      <a-col :span="10">
        <a-form-item label="工作单位" :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
          <span class="text_color" v-if="disable">{{ item.company }}</span>
          <a-input v-decorator="[`company-${index}`, { initialValue: item.company }]" v-else @change="change($event, index, 'company')" />
        </a-form-item>
      </a-col>
      <a-col :span="10">
        <a-form-item label="职务" :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
          <span class="text_color" v-if="disable">{{ item.position }}</span>
          <a-input
            v-decorator="[`position-${index}`, { initialValue: item.position }]"
            v-else
            @change="change($event, index, 'position')"
          />
        </a-form-item>
      </a-col>
      <a-col :span="10">
        <a-form-item label="工作开始时间" :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
          <span class="text_color" v-if="disable">{{ item.startTime }}</span>
          <a-date-picker
            style="width:100%"
            type="date"
            placeholder="选择日期"
            v-decorator="[`startTime-${index}`, { initialValue: getMoment(item.startTime) }]"
            v-else
            @change="change2($event, index, 'startTime')"
          />
        </a-form-item>
      </a-col>
      <a-col :span="10">
        <a-form-item label="工作结束时间" :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
          <span class="text_color" v-if="disable">{{ item.endTime }}</span>
          <a-date-picker
            style="width:100%"
            type="date"
            placeholder="选择日期"
            v-decorator="[`endTime-${index}`, { initialValue: getMoment(item.endTime) }]"
            v-else
            @change="change2($event, index, 'endTime')"
          />
        </a-form-item>
      </a-col>
      <a-col :span="10">
        <a-form-item label="证明人" :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
          <span class="text_color" v-if="disable">{{ item.witName }}</span>
          <a-input v-decorator="[`witName-${index}`, { initialValue: item.witName }]" v-else @change="change($event, index, 'witName')" />
        </a-form-item>
      </a-col>
      <a-col :span="10">
        <a-form-item label="证明人联系电话" :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
          <span class="text_color" v-if="disable">{{ item.witPhone }}</span>
          <a-input
            style="width:100%"
            v-decorator="[
              `witPhone-${index}`,
              {
                rules: [
                  { required: true, message: '联系电话必填' },
                  {
                    pattern: /^1[3456789]\d{9}$/,
                    message: '请正确输入手机号！'
                  }
                ],
                initialValue: item.witPhone
              }
            ]"
            v-else
            @change="change($event, index, 'witPhone')"
          />
        </a-form-item>
      </a-col>
      <a-col :span="10">
        <a-form-item label="离职原因" :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
          <span class="text_color" v-if="disable">{{ item.leaveReason }}</span>
          <a-input
            v-decorator="[`leaveReason-${index}`, { initialValue: item.leaveReason }]"
            v-else
            @change="change($event, index, 'leaveReason')"
          />
        </a-form-item>
      </a-col>
      <a-col :span="10">
        <a-form-item label="离职薪资" :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
          <span class="text_color" v-if="disable">{{ item.leaveSalary }}</span>
          <a-input
            v-decorator="[`leaveSalary-${index}`, { initialValue: item.leaveSalary }]"
            v-else
            @change="change($event, index, 'leaveSalary')"
          />
        </a-form-item>
      </a-col>
      <a-col :span="17">
        <a-form-item label="工作备注" :label-col="{ span: 3 }" :wrapper-col="{ span: 21 }">
          <span class="text_color" v-if="disable">{{ item.remark }}</span>
          <a-input
            type="textarea"
            v-decorator="[`remark-${index}`, { initialValue: item.remark }]"
            v-else
            @change="change($event, index, 'remark')"
          ></a-input>
        </a-form-item>
      </a-col>

      <a-col :span="10" :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
        <a-form-item :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
          <a-input v-decorator="[`id-${index}`, { initialValue: String(item.id) }]" type="hidden" />
        </a-form-item>
      </a-col>
      <a-col v-if="!disable" :span="17" class="mb24">
        <a-button type="dashed" style="width:100%;height:44px;" @click="delHandle(item)">删除</a-button>
      </a-col>
      <a-col v-if="index != empWork.empWorkList.length - 1" :span="17" class="border-top mb12"></a-col>
    </div>
    <div v-if="!disable">
      <a-col :span="17" class="border-top"></a-col>
      <a-col :span="17" class="mt24 mb24">
        <a-button type="dashed" style="width:100%;height:44px;" @click="addHandle">新增</a-button>
      </a-col>
      <a-col :span="23">
        <div class="basicInfo-button-group">
          <a-button class="mr12" @click="cancel">取消</a-button>
          <a-button type="primary" @click="submit">确认</a-button>
        </div>
      </a-col>
    </div>
  </a-form>
</template>
<script>
import { validPhone } from "@/utils/validate";
import { updateEmpAllInfo } from "@/api/staff/staff";
export default {
  name: "WorkForm",
  props: {
    empWork: {
      type: Object
    },
    disable: {}
  },
  data() {
    return {
      validPhone,
      form: this.$form.createForm(this),
      relationOptions: [{ value: "父母" }, { value: "配偶" }, { value: "子女" }, { value: "朋友" }, { value: "其他" }]
    };
  },
  watch: {
    empWork() {
      if (this.empWork.empWorkList === null || this.empWork.empWorkList.length === 0) {
        this.$set(this.empWork, "empWorkList", [{}]);
      }
    }
  },
  methods: {
    change(e, index, key) {
      this.$set(this.empWork.empWorkList, index, {
        ...this.empWork.empWorkList[index],
        [key]: e.target.value
      });
    },
    change1(e, index, key) {
      this.$set(this.empWork.empWorkList, index, {
        ...this.empWork.empWorkList[index],
        [key]: e
      });
    },
    change2(e, index, key) {
      this.$set(this.empWork.empWorkList, index, {
        ...this.empWork.empWorkList[index],
        [key]: this.getMoment(e)
      });
    },
    cancel() {
      this.$emit("cancelEdit");
      if (!this.empWork.empWorkList || this.empWork.empWorkList.length === 0) {
        this.empWork.empWorkList.push({ guid: Math.random() * 16 });
      }
    },
    addHandle() {
      if (this.empWork.empWorkList === null) {
        this.empWork.empWorkList = [];
      }
      this.empWork.empWorkList.push({ guid: Math.random() * 16 });
    },
    delHandle(item) {
      const index = this.empWork.empWorkList.indexOf(item);

      if (index !== -1) {
        this.empWork.empWorkList.splice(index, 1);
      }
    },
    getMoment(date) {
      return date ? this.moment(date) : null;
    },
    submit() {
      var that = this;
      this.form.validateFields((err, values) => {
        if (!err) {
          const value = this.beautyData(values);

          value.map(item => {
            item.leaveSalary = item.leaveSalary * 100;
          });
          setTimeout(() => {
            updateEmpAllInfo({
              id: this.empWork.id,
              phone: this.empWork.phone,
              empWorkList: value
            }).then(res => {
              that.$notification["success"]({
                message: "success",
                description: "编辑成功"
              });

              that.$emit("cancelEdit");
            });
          }, 300);
        }
      });
    },
    beautyData(data) {
      var arr = [];
      this.empWork.empWorkList.forEach((item, index) => {
        arr[index] = {
          company: data[`company-${index}`],
          position: data[`position-${index}`],
          witName: data[`witName-${index}`],
          witPhone: data[`witPhone-${index}`],
          startTime: this.getMoment(data[`startTime-${index}`]),
          endTime: this.getMoment(data[`endTime-${index}`]),
          textarea: data[`textarea-${index}`],
          leaveSalary: data[`leaveSalary-${index}`],
          remark: data[`remark-${index}`],
          leaveReason: data[`leaveReason-${index}`],
          id: data[`id-${index}`]
        };
      });
      return arr;
    }
  }
};
</script>

<style scoped>
.basicInfo-button-group {
  text-align: center;
  padding-bottom: 24px;
}
</style>
